﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WHControlLib.Controls
{
    //****************************说明**************************************
    // *******************进度条的进度通过 Value属性设置*******************
    //*****要想获得本进度条的当前进度的百分比值通过只读属性 Valuepercentage 获得*****
    //*****当制作仪表板盘控件时方法基本相同可继承然后部分重绘***************
    public partial class WHCircleProgessBar : WHProgressBar
    {
        public WHCircleProgessBar()
        {
            InitializeComponent();
        }
        //全局定义
      public  Rectangle DrawRect = new Rectangle();
        public int CircleValue;

        #region 属性定义
        private float _inCircleBl=0.6f;
        [Category("A我的圆形"), Description("圆形进度条内圈的比例，默认，0.8f 不能小于0.1大于0.95"), Browsable(true)]
        public float InCircleBl
        {
            get { return _inCircleBl; }
            set {
                if (value>0.95|| value<0.1)
                {
                    value = 0.8f;
                }
                else
                _inCircleBl = value; Invalidate();
            }
        }
        private Color _inCircleFirstColor=Color.Bisque;
        [Category("A我的圆形"), Description("圆形进度条内圈第一种填充色，默认 浅橙色"), Browsable(true)]
        public Color InCircleFirstColor
        {
            get { return _inCircleFirstColor; }
            set { _inCircleFirstColor = value; Invalidate(); }
        }

        private Color _inCircleSecondColor=Color.DarkOrange;
        [Category("A我的圆形"), Description("圆形进度条内圈第二种填充色，默认,深橙色"), Browsable(true)]
        public Color InCircleSecondColor
        {
            get { return _inCircleSecondColor; }
            set { _inCircleSecondColor = value; Invalidate(); }
        }



        private Color _informCircleColor = Color.Red;
        [Category("A我的圆形"), Description("圆形进度条信息提示环的颜色，内圈的外环，编程时可令其随进度变色，默认,红色"), Browsable(true)]
        public Color InformCircleColor
        {
            get { return _informCircleColor; }
            set { _informCircleColor = value; Invalidate(); }
        }
        private float _valuegradualchange1=0.15f;
        [Category("A我的圆形"), Description("圆形进度条渐变色调节项1，默认 0.15 必须大于0小于1"), Browsable(true)]
        public float Valuegradualchange1
        {
            get { return _valuegradualchange1; }
            set
            {
                if (value>=1||value<=0)
                {
                    _valuegradualchange1 = 0.15f;
                }
               else _valuegradualchange1 = value; Invalidate();
            }
        }

        private float _valuegradualchange2 = 0.85f;
        [Category("A我的圆形"), Description("圆形进度条渐变色调节项2，默认 0.85 必须大于0小于1"), Browsable(true)]
        public float Valuegradualchange2
        {
            get { return _valuegradualchange2; }
            set
            {
                if (value >= 1 || value <= 0)
                {
                    _valuegradualchange2 = 0.15f;
                }
                else _valuegradualchange2 = value; Invalidate();
            }
        }
        private float _valuegradualchange3 = 0.15f;
        [Category("A我的圆形"), Description("圆形进度条渐变色调节项3，主要用这项增减扩散效果，默认 0.15 必须大于0小于1"), Browsable(true)]
        public float Valuegradualchange3
        {
            get { return _valuegradualchange3; }
            set
            {
                if (value >= 1 || value <= 0)
                {
                    _valuegradualchange3 = 0.15f;
                }
                else _valuegradualchange3 = value; Invalidate();
            }
        }
        //线帽样式
     
        private    LineCap _myLineCap=LineCap.Round;
        [Category("A我的圆形"), Description("进度条两头的线帽样式，默认,圆头线帽"), Browsable(true)]
        public LineCap MyLineCap

        {
            get { return _myLineCap; }
            set { _myLineCap = value; Invalidate(); }
        }


        //        private Color _valuePgSecondColor=Color.Black;
        //[Category("A我的圆形"), Description("圆形进度条当前值进度条的第一种颜色，默认,灰色"), Browsable(true)]
        //public Color ValuePgSecondColor
        //{ 
        //    get { return _valuePgSecondColor; }
        //    set { _valuePgSecondColor = value; Invalidate(); }
        //}

        #endregion

        public override void BiforePainIni()
        {
            Width = Height;
            MyRect = ClientRectangle;
            int Penwidth = BorderWidth / 2;
            DrawRect.X = MyRect.X + Penwidth;
            DrawRect.Y = MyRect.Y + Penwidth;
            DrawRect.Height = (int)(Height - Penwidth * 2);
            DrawRect.Width = (int)(MyRect.Width - Penwidth * 2);
        }
        


        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

            BiforePainIni();
            //当 作图矩形的高度小于等于0时候退出，不然异常
            if (DrawRect.Height<=0)
            {
                return;
            }

            Graphics Myg = e.Graphics;
            Myg.SmoothingMode = SmoothingMode.AntiAlias;
            Myg.CompositingQuality = CompositingQuality.HighQuality;
            Myg.InterpolationMode = InterpolationMode.HighQualityBicubic;
            try
            {
                //画进度条的背景大圆形状和填充
                DrawProgessInside(Myg, DrawRect);

                //画填充进度条
                DrawValueProgess(Myg, DrawRect);

                //填充内部小圆的颜色
                DrawSmallCicleBackground(Myg, DrawRect);

                //画整体边框
                if (IsShowBorder)
                {

                    DrawProgessBorder(Myg, DrawRect, BorderWidth);
                }


                //画出百分比文字
                if (IsShowText)
                {
                    progessValueStr = GetDisplayText();
                    DrawProgessText(Myg, DrawRect, progessValueStr);
                }

            }
            catch    {}       


        }
        /// <summary>
        /// 画圆形进度条的背景
        /// </summary>
        /// <param name="Myg"></param>
        /// <param name="myrect">控件显示区域</param>
        public override void DrawProgessInside(Graphics Myg, Rectangle myrect)
        {
            //base.DrawProgessInside(Myg, MyRect);
            //确定内圈的矩形
            Rectangle Inmyrect = new Rectangle();
             Inmyrect.Height=(int)(myrect.Height*InCircleBl);
            Inmyrect.Width = Inmyrect.Height;
            Inmyrect.X = (int)(myrect.X+myrect.Width/2-Inmyrect.Width/2);
            Inmyrect.Y=(int)(myrect.Y+myrect.Height/2-Inmyrect.Height/2);

            GraphicsPath backgroundOutPath= new GraphicsPath();
            backgroundOutPath.AddEllipse(myrect);
            if (IsUseShadow)
            {
      
                    using (LinearGradientBrush ingroundBrush = new LinearGradientBrush(myrect, BackgroundColor, InsideShadowColor, LinearGradientMode.ForwardDiagonal))
                    {
                        Myg.FillPath(ingroundBrush, backgroundOutPath);
                    }

            }
            //不启用双色时候
            else
            {
                
                    using (SolidBrush ingroundBrush = new SolidBrush(BackgroundColor))
                    {
                        Myg.FillPath(ingroundBrush, backgroundOutPath);
                    }
             
                }
            }

       
        void DrawSmallCicleBackground(Graphics Myg,Rectangle myrect)
        {
            //确定内圈的矩形
            Rectangle Inmyrect = new Rectangle();
            Inmyrect.Height = (int)(myrect.Height * InCircleBl);
            Inmyrect.Width = Inmyrect.Height;
            Inmyrect.X = (int)(myrect.X + myrect.Width / 2 - Inmyrect.Width / 2);
            Inmyrect.Y = (int)(myrect.Y + myrect.Height / 2 - Inmyrect.Height / 2);
            GraphicsPath backgroundInPath = new GraphicsPath();
            

            backgroundInPath.AddEllipse(Inmyrect);

            //启用双色时候内部显示区域自动启用渐变色
            if (IsUseTwoColor)
            {
          
                    using (LinearGradientBrush ingroundBrush = new LinearGradientBrush(Inmyrect, InCircleFirstColor, InCircleSecondColor, LinearGradientMode.Vertical))
                    {
                        Myg.FillPath(ingroundBrush, backgroundInPath);
                    }

                }

            //不启用双色色时候
            else
            {
             
                    using (SolidBrush ingroundBrush = new SolidBrush(InCircleFirstColor))
                    {
                        Myg.FillPath(ingroundBrush, backgroundInPath);
                    }

                }
            }



  /// <summary>
  /// 画圆形进度条的进度
  /// </summary>
  /// <param name="Myg"></param>
  /// <param name="myrect"></param>
        public override void DrawValueProgess(Graphics Myg, Rectangle myrect)
        {
        
            //确定内圈的矩形
            Rectangle Inmyrect = new Rectangle();
            Inmyrect.Height = (int)(myrect.Height * InCircleBl);
            Inmyrect.Width = Inmyrect.Height;
            Inmyrect.X = (int)(myrect.X + myrect.Width / 2 - Inmyrect.Width / 2);
            Inmyrect.Y = (int)(myrect.Y + myrect.Height / 2 - Inmyrect.Height / 2);
            //得到文字显示的矩形区域
            ProgressTextRect = Inmyrect;
         
            //确定进度条的两个填充矩形圆环
            //进度条内的矩形颜色环 ，将来用于变色提示
            ///  画进度值的矩形
            Rectangle outBigRect = new Rectangle();
          //画信息提示环的矩形
            Rectangle inSmallRect = new Rectangle();
            int H = (int)(myrect.Height * (1 - InCircleBl) /3);
            int inH = H / 2;
           
        
            inSmallRect.X = Inmyrect.X- inH;
            inSmallRect.Y = Inmyrect.Y-inH;
            inSmallRect.Width =Inmyrect.Width+inH * 2;
            inSmallRect.Height = Inmyrect.Height+inH*2;
            //进度条的填充矩形
            outBigRect.X = inSmallRect.X - inH ;
            outBigRect.Y = inSmallRect.Y-inH ;
            outBigRect.Width = inSmallRect.Width + inH * 2;
            outBigRect.Height = inSmallRect.Height + inH * 2;
           
            //确定画进度条的画笔的画笔 的 宽度
             float BrushPenWidth = (int)(outBigRect.Height-inSmallRect.Height);
           
            if (outBigRect.Height>Height||inSmallRect.Height>Height)
            {
                outBigRect.Height = Height;
                inSmallRect.Height = Height-(int) BrushPenWidth;

            }
            GraphicsPath ValueOutPath = new GraphicsPath();
            GraphicsPath ValueInPath = new GraphicsPath();
            float f = (float)Value / Maximum;
            if (f>1)   f = 1.0f;
            if (f<0) f = 0;
            //value值对应的度数
            int Du = (int)(f * 360);
            if (Du>0)
            { 

                if (IsUseTwoColor)
                { 
                    // 采用路径渐变实现环绕渐变效果
                GraphicsPath circlePath = new GraphicsPath();
                Rectangle pathBrushRect = new Rectangle();
                pathBrushRect.X = outBigRect.X - inH;
                pathBrushRect.Y = outBigRect.Y - inH;
                pathBrushRect.Width = outBigRect.Width + inH * 2;
                pathBrushRect.Height = outBigRect.Height + inH * 2;
                circlePath.AddEllipse(pathBrushRect);
                    using (PathGradientBrush b = new PathGradientBrush(circlePath))
                {
                    //定义路径渐变的方式和程度
                    b.CenterPoint = new Point(myrect.Width / 2, myrect.Height / 2);
                    b.CenterColor = FirstFillcolor;
                      //圆弧渐变的渐变程度 目前没有提供调节选项
                   b.RotateTransform(Valuegradualchange1);
                    b.SetBlendTriangularShape(Valuegradualchange2);
                    b.SetSigmaBellShape(Valuegradualchange3);
                    b.SurroundColors = new Color[] {SecondFillcolor};
                        using (Pen spen = new Pen(b, BrushPenWidth) )
                    {
                       // 启用圆头线帽
                        spen.EndCap = LineCap.Round;
                        spen.StartCap = LineCap.Round;
                        Myg.DrawArc(spen, outBigRect, 90, Du);
                        
                        }


                }
                }
                else
                {
                    using (Pen solidPen=new Pen(FirstFillcolor,BrushPenWidth))
                    {

                        // 启用圆头线帽
                        solidPen.EndCap = MyLineCap;
                        solidPen.StartCap = MyLineCap;
                        
                        Myg.DrawArc(solidPen, outBigRect, 90, Du);
                    }
                }



            }



            //画 隔离进度条与内圈的环形
            using (SolidBrush sb = new SolidBrush(InformCircleColor))
            {
                Myg.FillEllipse(sb, inSmallRect);
 
            }
        }

       string GetDisplayText()
        {
              string valeText = "0%";
                  float circlevalue=0;
                 if (Maximum<=Minimum)
                {
                _valuepercentage = 100;
               return  valeText = "100%";
                 }
            if (Value<=Minimum)
            {
                _valuepercentage = 0;
                return "0%";
            }
            if (Value>=Maximum)
            {
                _valuepercentage = 100;
                return "100%";
            }
            
             circlevalue =Math.Abs(( (float)(Value - Minimum) / (Maximum-Minimum)));
            if (circlevalue >= 1)
            {
                _valuepercentage = 100;
               return "100%";
            }
              
            else
            {
                _valuepercentage = Convert.ToInt32(circlevalue * 100);
                valeText = (circlevalue * 100).ToString("#0") + "%";
               
                return valeText;
            }
        }


        public override void DrawProgessText(Graphics Myg, Rectangle myrect, string progessText)
        {

            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            Rectangle TextRect = new Rectangle();
            TextRect = ProgressTextRect;
            ///当文字大于显示区域时候不显示文字
            //SizeF textSize = Myg.MeasureString(progessText, ProgessTextFont);
            ///
            //if ((int)textSize.Width > TextRect.Width)
            //{
            //    progessText = "";
            //    return;
            //}
            using (SolidBrush textbrush = new SolidBrush(ProgessTextColor))
            {
                Myg.DrawString(progessText, ProgessTextFont, textbrush, TextRect, sf);
            }






        }

        public override void DrawProgessBorder(Graphics Myg, Rectangle myrect, int Bwidth)
        {
            //base.DrawProgessBorder(Myg, myrect, Bwidth);
            //确定内圈的矩形
            Rectangle Inmyrect = new Rectangle();
            Inmyrect.Height = (int)(myrect.Height * InCircleBl);
            Inmyrect.Width = Inmyrect.Height;
            Inmyrect.X = (int)(myrect.X + myrect.Width / 2 - Inmyrect.Width / 2);
            Inmyrect.Y = (int)(myrect.Y + myrect.Height / 2 - Inmyrect.Height / 2);

            GraphicsPath backgroundOutPath = new GraphicsPath();
            GraphicsPath backgroundInPath = new GraphicsPath();
            backgroundOutPath.AddEllipse(myrect);

            backgroundInPath.AddEllipse(Inmyrect);
            using (Pen bpen = new Pen(BorderColor, BorderWidth))
            {
              
                Myg.DrawPath(bpen, backgroundOutPath);
            }
            using (Pen Inbpen = new Pen(BackgroundColor, BorderWidth))
            {
                Myg.DrawPath(Inbpen, backgroundInPath);
      
            }
        }


        ///////////////////////////////////////////////////////////////////////////////////

    }
}
